|
|
|
|
|
|
|
pute the values for the last column. Once again, you can save a little extra time by computing each entry in the resulting matrix without using nested For loops. |
|
|
|
|
|
|
|
|
The following Visual Basic subroutine uses these facts to multiply two matrices. In some tests, a similar subroutine that did not take advantage of the special structure of the matrices took more than three times as long to perform the same calculations. |
|
|
|
 |
|
|
|
|
' Multiply two transformation matrices. Public Sub m2MatMultiply(Result() As Single, A() As Single, _
B() As Single)
Result(1, 1) = A(1, 1) * B(1, 1) + A(1, 2) * B(2, 1)
Result(1, 2) = A(1, 1) * B(1, 2) + A(1, 2) * B(2, 2)
Result(1, 3) = 0#
Result(2, 1) = A(2, 1) * B(1, 1) + A(2, 2) * B(2, 1)
Result(2, 2) = A(2, 1) * B(1, 2) + A(2, 2) * B(2, 2)
Result(2, 3) = 0#
Result(3, 1) = A(3, 1) * B(1, 1) + A(3, 2) * B(2, 1) + _
B(3, 1)
Result(3, 2) = A(3, 1) * B(1, 2) + A(3, 2) * B(2, 2) + _
B(3, 2)
Result(3, 3) = 1#
End Sub |
|
|
|
|
|
|
|
|
You can find these and other routines for manipulating two-dimensional matrices and vectors in the file M2Ops.bas on the CD-ROM. |
|
|
|
|
|
|
|
|
Once you have created a transformation, you might like to apply it to the drawing objects described earlier in this chapter. For each object class, create a Transform subroutine that applies a transformation matrix to the object. For instance, the following Visual Basic code applies a transformation matrix to a TwoDPolygon object with points stored in the m_Points array. |
|
|
|
 |
|
|
|
|
' Transform the object using a two-dimensional
' transformation matrix. Public Sub Transform(M() As Single)
Dim i As Integer
Dim new_x As Single
Dim new_y As Single
For i = 1 To m_NumPoints
With m_Points(i)
new_x = .X * M(1, 1) + .Y * M(2, 1) + M(3, 1)
new_y = .X * M(1, 2) + .Y * M(2, 2) + M(3, 2)
.X = new_x
.Y = new_y
End With |
|
|
|
|
|